import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'

/**
 * Vite配置
 * 支持环境变量和路径别名、自动导入组件和API
 */
export default defineConfig({
  plugins: [
    vue(),
    // 自动导入API
    AutoImport({
      // 自动导入Vue相关API
      imports: ['vue', 'vue-router'],
      // 生成类型声明文件
      dts: 'src/auto-imports.d.ts',
    }),
    // 自动导入组件
    Components({
      // 指定组件位置
      dirs: ['src/components'],
      // 生成类型声明文件
      dts: 'src/components.d.ts',
    }),
  ],
  // 解析配置
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src'),
      '@components': resolve(__dirname, 'src/components'),
      '@assets': resolve(__dirname, 'src/assets'),
      '@api': resolve(__dirname, 'src/api'),
      '@ui': resolve(__dirname, '../../packages/ui/src')
    }
  },
  // 服务器配置
  server: {
    port: 3000,
    open: true,
    proxy: {
      '/api': {
        target: 'http://localhost:8080',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      }
    }
  },
  // 构建配置
  build: {
    outDir: 'dist',
    emptyOutDir: true,
    reportCompressedSize: false
  }
})
